package com.jsoup;
import java.io.IOException;   
import java.io.UnsupportedEncodingException;   
import java.net.MalformedURLException;   
  
import org.jsoup.Jsoup;   
import org.jsoup.nodes.Document;   
import org.jsoup.nodes.Element;   
  
  
/**  
* 获取优酷视频  
* @author honny.huang  
* 2011-3-29  
* http://hi.juziku.com/sunlightcs/  
*/  

public class JsoupParseYouKu 
{
	 public static void main(String[] args) throws Exception
	 {   
	    Document doc = getURLContent();   
	    if (null != doc && doc.data().length()> 0) 
	    {
	    	Element titleEle = doc.getElementsByTag("title").first();
			String titleStr = titleEle.text().toString();
			System.out.println("视频标题：" + titleStr);
			
	    	String pic = getElementAttrById(doc, "s_sina", "href");   
	        int local = pic.indexOf("pic=");   
	        pic = pic.substring(local+4);   
	        System.out.println("视频缩略图："+pic);   
	        
	        String flashUrl = getElementAttrById(doc, "link2", "value");   
	        System.out.println("视频地址："+flashUrl);   
	           
	        String time = getElementAttrById(doc, "download", "href");   
	        String []arrays = time.split("\\|");   
	        time = arrays[4];   
	        System.out.println("视频时长："+time);   
	        
	        //视频缩略图的另一种获取方法
	        String picString =  arrays[8];
	        System.out.println(picString);
	    	
	    }
	 }   
	       
	       
	    /**  
	     * 根据HTML的ID键及属于名，获取属于值  
	     * @param id  HTML的ID键  
	     * @param attrName  属于名  
	     * @return  返回属性值  
	     */  
	    private static String getElementAttrById(Document doc, String id, String attrName)throws Exception{   
	       
	        Element et = doc.getElementById(id);   
	        String attrValue = et.attr(attrName);   
	           
	        return attrValue;   
	    }   
	       
	  
	       
	    /**  
	     * 获取优酷网页的内容  
	     */  
	    private static Document getURLContent() throws MalformedURLException, IOException, UnsupportedEncodingException 
	    {   
	        Document doc = Jsoup.connect("http://v.youku.com/v_show/id_XMjU0MjI2NzY0.html")   
	          .data("query", "Java")   
	          .userAgent("Mozilla")   
	          .cookie("auth", "token")   
	          .timeout(3000)   
	          .post();   
	        return doc;   
	    }   


}
